package cn.yunhe.action;

import cn.yunhe.entity.Permission;
import cn.yunhe.entity.Role;
import cn.yunhe.service.PermissionBiz;
import cn.yunhe.service.RoleBiz;
import cn.yunhe.util.BaseActionUtil;
import cn.yunhe.util.UUIDUtil;
import com.opensymphony.xwork2.ModelDriven;
/*import org.apache.struts2.convention.annotation.Action;*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RoleAction extends BaseActionUtil implements ModelDriven {

    @Autowired
    @Qualifier(value = "roleBiz")
    private RoleBiz roleBiz;

    private Role role;//模型驱动
    private List<Role>roleList;//JSON返回的数据类型

    private String str2Pids;

    @Autowired
    @Qualifier(value = "permissionBiz")
    private PermissionBiz permissionBiz;

    List<Permission>permissionList;

    private Map<String,Object>objectMap2Json=new HashMap<>();

    public String searchByConditions(){
        Map<String,Object>objectMap=new HashMap<>();
        objectMap.put("role",role);
        roleList=roleBiz.searchByConditions(objectMap);
        return "SUCCESS2searchByConditions";
    }

    public String update2Role2Permissons(){
        System.out.println("getRid====="+role.getRid());
        System.out.println("str2Pids====="+str2Pids);

        Map<String,Object>objectMap=new HashMap<>();

        //删除旧的信息
        objectMap.put("rprid",role.getRid());
        int num2Delete2Permissions=roleBiz.delete2Role_Permissions(objectMap);
        System.out.println("num2Delete2Permissions==="+num2Delete2Permissions);

        String[] pid2StrArray=str2Pids.split(",");
        int[] pid2Array=new int[pid2StrArray.length];
        for (int i=0;i<pid2Array.length;i++){
            pid2Array[i]=Integer.parseInt(pid2StrArray[i]);
            System.out.println(pid2Array[i]);
            objectMap.put("rppid",pid2Array[i]);
            objectMap.put("rpid",UUIDUtil.getUUID());
            int num2Insert2Permissions= roleBiz.insert2Role_Permissions(objectMap);
            System.out.println(i+"===num2Insert2Permissions====="+num2Insert2Permissions);
        }

        System.out.println("rid======="+objectMap.get("rprid"));
        objectMap.put("rid",objectMap.get("rprid"));
        permissionList=permissionBiz.searchByRid(objectMap);
        System.out.println("role的更新后===permissionList==="+permissionList);

        System.out.println("objectMap2Json===="+objectMap2Json);

        objectMap2Json.put("role111",role);
        objectMap2Json.put("str2Pids111",str2Pids);
        objectMap2Json.put("permissionList111",permissionList);
        return "SUCCESS2update2Role2Permissons";
    }

    public RoleBiz getRoleBiz() {
        return roleBiz;
    }

    public void setRoleBiz(RoleBiz roleBiz) {
        this.roleBiz = roleBiz;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public String getStr2Pids() {
        return str2Pids;
    }

    public void setStr2Pids(String str2Pids) {
        this.str2Pids = str2Pids;
    }

    public PermissionBiz getPermissionBiz() {
        return permissionBiz;
    }

    public void setPermissionBiz(PermissionBiz permissionBiz) {
        this.permissionBiz = permissionBiz;
    }

    public List<Permission> getPermissionList() {
        return permissionList;
    }

    public void setPermissionList(List<Permission> permissionList) {
        this.permissionList = permissionList;
    }

    public Map<String, Object> getObjectMap2Json() {
        return objectMap2Json;
    }

    public void setObjectMap2Json(Map<String, Object> objectMap2Json) {
        this.objectMap2Json = objectMap2Json;
    }

    @Override
    public Role getModel() {
        if (role==null)
            role=new Role();
        return role;
    }
}
